<?php

namespace App\Http\Controllers\Internal;

use App\Models\UserWeeklyStats;

class WeeklyStatController extends Controller
{
    /*
     * @api post /internal/weeklystats 创建用户周报记录或者更新
     * @version 0.0.1
     * @group internal
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{
     *  }
     * }
     */
    public function store()
    {
        $this->validate($this->request, [
            'userId' => 'required|integer',
            'week' => 'required|integer',
            'incrWordCount' => 'integer',
            'startDate' => 'required|string',
            'endDate' => 'required|string',
        ]);
        $userId = $this->request->input('userId');
        $week = $this->request->input('week');
        $incrWordCount = $this->request->input('incrWordCount', 0);
        $startDate = $this->request->input('startDate');
        $endDate = $this->request->input('endDate');

        /** @var UserWeeklyStats $statObj */
        $statObj = UserWeeklyStats::firstOrNew([
            'user_id' => $userId,
            'week' => $week,
        ]);
        if ($incrWordCount) {
            $statObj->incrWordCount($incrWordCount);
        }
        $statObj->setStartData($startDate);
        $statObj->setEndData($endDate);
        $statObj->save();

        return $this->sendSuccessJson();
    }

}
